#!/usr/bin/env bash

source ./include/fun.sh

if [ '' == ${JAVA_HOME} ]; then
    println "JAVA_HOME not set";
fi
if [ '' == ${ANDROID_HOME} ]; then
    println "ANDROID_HOME not set";
fi
if [ '' == `npm -v` ]; then
    println "cordova not set";
fi
if [ '' == `cordova -v` ]; then
    println "cordova not set";
fi


plugins=(
    cordova-plugin-console
    cordova-plugin-contacts
    cordova-plugin-camera
    cordova-plugin-battery-status
    cordova-plugin-device
    cordova-plugin-file
    cordova-plugin-file-transfer
    cordova-plugin-geolocation
    "git+https://github.com/phonegap/phonegap-plugin-barcodescanner.git"
    cordova-plugin-dialogs
    cordova-plugin-whitelist
)


case ${1} in
"devices")
    adb devices
    ;;
"create")
    # 访问上级目录
    appdir=${WORKSPACE_DIR}/app/
    buildir ${appdir}
    cd ${appdir}
    # 项目不存在的情况下初始化
    appname=${2}
    if [ -d ${appdir}/${appname} ]; then
        println "app ${appname} exist"
    else
        cordova create ${appname} ${appname}
        println "app ${appname} create success"
        # cordova 插件安装
        cd ${appname}
        i=0
        while(( ${i} < ${#plugins[*]} ))
        do
            println "installing cordova plugins of ${plugins[${i}]}"
            cordova plugins add ${plugins[${i}]}
            let "i++"
        done
        cordova platforms add android
        cordova platforms add ios
    fi
    ;;
"compile")
    println '<preference name="iosExtraFilesystems" value="library,library-nosync,documents,documents-nosync,cache,bundle,root" />';
    println '<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,assets,root" />';
    ;;
"run")
    cd ${WORKSPACE_DIR}/app/${2}
    if [ $# == 3 ];then
        TARGET="-target=$3"
    else
        TARGET=""
    fi
    cordova run android ${TARGET}
    ;;
"release")
    appname=${2}
    cd ${appname}
    if [ -n ${3} ]; then
        PLATFORM=android
    else
        PLATFORM=${3}
    fi
    buildpath=${WORKSPACE_DIR}/app/${appname}/platforms/android/build/outputs/apk/android-release.apk
    releasepath=${WORKSPACE_DIR}/app/${appname}.apk
    if [ -f ${buildpath} ]; then
        unlink ${buildpath}
    fi
    if [ -f ${releasepath} ]; then
        unlink ${releasepath}
    fi
    cordova build ${PLATFORM} --release
    cd ${WORKSPACE_DIR}/app
    echo -e "------------------- ${WORKSPACE_DIR} ------------------------------"
    if [ -f ${buildpath} ]; then
        cp ${buildpath} ${releasepath}
        echo -e "success to build release "
    else
        echo -e "failed to build release "
    fi
    ;;
"install")
    DEVICE=${2}
    APK=${3}
    adb -s ${DEVICE} install ${APK}
    ;;
"uninstall")
    DEVICE=${2}
    PACKAGE=${3}
    adb -s ${DEVICE} uninstall ${PACKAGE}
    ;;
"switch")
    NEW_PACKAGE=${2}
    OLD_PACHAGE=`cat package.info`

    message=""
    if [ ${NEW_PACKAGE} != ${OLD_PACHAGE} ]; then
        if [ -d www ]; then
            rm -f package.info
            mv www projects/${OLD_PACHAGE}
            mv releasekey.keystore projects/${OLD_PACHAGE}.keystore
            mv config.xml projects/${OLD_PACHAGE}.xml

            if [ -d projects/${NEW_PACKAGE} ]; then
                echo ${NEW_PACKAGE} > package.info
                mv projects/${NEW_PACKAGE} www
                mv projects/${NEW_PACKAGE}.keystore releasekey.keystore
                mv projects/${NEW_PACKAGE}.xml config.xml
            else
                message="projects/${NEW_PACKAGE} not exist"
            fi
            message="success"
        else
            message="www not exist"
        fi

    else
        message="skip"
    fi
    println "switch ${OLD_PACHAGE} to ${NEW_PACKAGE} : ${message}"
    ;;
"build")
    KEYSTORE=${2}
    ALIAS=cordova
    STOREPASSWORD=${3}
    PASSWORD=${4}
    echo "cordova build android --release  --keystore=${KEYSTORE} --alias=${ALIAS}   --storePassword=${STOREPASSWORD} --password=${PASSWORD}"
    cordova build android --release  --keystore=${KEYSTORE} --alias=${ALIAS}   --storePassword=${STOREPASSWORD} --password=${PASSWORD}
    ;;
esac

